package com.yonyougov.devops.cruxcisupport.versioninfo;

import com.yonyougov.devops.cruxcisupport.versioninfo.dto.*;
import com.yonyougov.devops.cruxcisupport.versioninfo.vo.VersionInfoPageVo;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ligson on 2017/8/11.
 */
@RestController
@RequestMapping("/api/v2/versioninfo")
public class VersionInfoController extends CrudController<VersionInfoEntity, BigInteger> {

    private final VersionInfoService versionInfoService;


    public VersionInfoController(VersionInfoService versionInfoService) {
        this.versionInfoService = versionInfoService;
    }

    @Override
    public BaseService<VersionInfoEntity, BigInteger> getService() {
        return versionInfoService;
    }

    //<!--用户自定义代码开始-->
    @ApiOperation(value = "添加版本信息", notes = "添加版本信息")
    @PostMapping(value = "/vi/add")
    public BaseResponseDto addVersionInfo(@RequestBody @Valid VersionInfoAddRequestDto requestDto) {
        VersionInfoEntity versionInfoEntity = new VersionInfoEntity();
        BeanUtils.copyProperties(requestDto, versionInfoEntity);

        versionInfoService.add(versionInfoEntity);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "更新版本信息", notes = "更新版本信息")
    @PostMapping(value = "/vi/update")
    public BaseResponseDto updateVersionInfo(@RequestBody @Valid VersionInfoUpdateRequestDto requestDto) {
        VersionInfoEntity versionInfoEntity = new VersionInfoEntity();
        BeanUtils.copyProperties(requestDto, versionInfoEntity);

        versionInfoService.update(versionInfoEntity);

        return BaseResponseDto.getSuccessInstance();
    }


    @ApiOperation(value = "分页查询版本信息", notes = "根据应用名称和版本名称")
    @PostMapping(value = "/vi/page/app")
    public BaseResponseDto findVersionInfoPageByApp(@RequestBody VersionInfoAppPageReqDto requestDto) throws Exception {
        VersionInfoPageDto versionInfoPageDto = new VersionInfoPageDto();
        BeanUtils.copyProperties(requestDto, versionInfoPageDto);

        Pagination<VersionInfoPageVo> pagination = versionInfoService.findPageBy(versionInfoPageDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", pagination.getDatas());
        result.put("totalCount", pagination.getTotalCount());
        return result;
    }

    @ApiOperation(value = "删除版本信息", notes = "根据版本信息主键")
    @PostMapping(value = "/vi/delete/id")
    public BaseResponseDto deleteVersionInfoById(@RequestBody @Valid DeleteVersionInfoRequestDto requestDto) {
        versionInfoService.deleteByPrimaryKey(requestDto.getId());

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "关联提测单", notes = "关联提测单")
    @PostMapping(value = "/vi/relation/ids")
    public BaseResponseDto relationVersionInfoById(@RequestBody @Valid RelationVersionInfoRequestDto requestDto) {
        VersionInfoEntity versionInfoEntity = new VersionInfoEntity();
        versionInfoEntity.setCommitTestId(requestDto.getCommitTestId());
        for (BigInteger versionInfoId:
        requestDto.getVersionIds()) {
            versionInfoEntity.setId(versionInfoId);
            versionInfoService.update(versionInfoEntity);
        }
        return BaseResponseDto.getSuccessInstance();
    }
    @ApiOperation(value = "查看所有未关联提测单版本", notes = "关联提测单")
    @PostMapping(value = "/vi/list")
    public BaseResponseDto fandListNoRelation() {
        VersionInfoPageDto versionInfoPageDto = new VersionInfoPageDto();
        Pagination<VersionInfoPageVo> pagination = versionInfoService.findNotRelation();
        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", pagination.getDatas());
        result.put("totalCount", pagination.getTotalCount());
        return result;
    }
    //<!--用户自定义代码结束-->
}
